home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / npurge11 / npurge.c < prev    next >
C/C++ Source or Header  |  1996-07-10  |  6KB  |  257 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <nit.h>
  5. #include <ntt.h>
  6. #include "ndwalk.h"
  7.  
  8.  
  9. #define ABORT(msg) \
  10.     fprintf(stderr,"%s at %d in %s\n",msg,__LINE__,__FILE__); \
  11.     ExitWithUsage();
  12.     
  13.  
  14. unsigned
  15.     currentDrive;
  16.  
  17. BYTE
  18.     baseDirectoryHandle,
  19.     workDrive;
  20.     
  21. WORD
  22.     serverConnID;
  23.     
  24. long
  25.     sequenceNumber;
  26.  
  27. NWSALV_ENTRY
  28.     dirEntry;
  29.  
  30. struct tm ts;
  31.  
  32. time_t
  33.     currentTime,
  34.     deleteTime;
  35.  
  36. double
  37.     seconds,
  38.     defaultTarget = 60.0 * 60.0 * 24.0 * 7.0,
  39.     target = 0.0;
  40.  
  41. char
  42.     directoryPathName[255],
  43.     scratchPad[255],
  44.     useDefaultTarget = 1,
  45.     processSubdirectories = 0,
  46.     reportOnly = 0,
  47.     forceMidnight = 0,
  48.     *rootDirectory = NULL,
  49.     originalPathname[255],
  50.     savePathname[255],
  51.     tempName[13];
  52.  
  53.  
  54. time_t TranslateDosDateTime(WORD dsrc,WORD tsrc,struct tm *tstruct)
  55. {
  56. time_t returnValue;
  57.  
  58.     tstruct->tm_year = (dsrc >> 9) + 80;
  59.     tstruct->tm_mon  = ((dsrc >> 5) & 0x000f) - 1;
  60.     tstruct->tm_mday = (dsrc & 0x1f);
  61.  
  62.     if (forceMidnight) {
  63.         tstruct->tm_hour = 0;
  64.         tstruct->tm_min = 0;
  65.         tstruct->tm_sec = 0;
  66.     }
  67.     else {
  68.         tstruct->tm_hour = (tsrc >> 11);
  69.         tstruct->tm_min  = ((tsrc >> 5) & 0x003f);
  70.         tstruct->tm_sec  = ((tsrc & 0x001f) * 2);
  71.     }
  72.     returnValue = mktime(tstruct);
  73.  
  74.     tstruct->tm_hour = (tsrc >> 11);
  75.     tstruct->tm_min  = ((tsrc >> 5) & 0x003f);
  76.     tstruct->tm_sec  = ((tsrc & 0x001f) * 2);
  77.     mktime(tstruct);
  78.  
  79.     return(returnValue);    
  80. }
  81.  
  82.  
  83. PurgeFiles(BYTE directoryHandle)
  84. {
  85. int result;
  86. WORD dtime, ddate;
  87.  
  88.     memset((char*)&dirEntry,0,sizeof(dirEntry));
  89.     result = ScanSalvagableFiles(serverConnID,directoryHandle,&sequenceNumber,&dirEntry);
  90.     if (result) return(0);
  91.  
  92.     ddate = *( ((WORD *)(&dirEntry.deletedDateAndTime))+1 );
  93.     dtime = *( (WORD *)(&dirEntry.deletedDateAndTime) );
  94.  
  95.     deleteTime = TranslateDosDateTime(ddate,dtime,&ts);
  96.     seconds = difftime(currentTime,deleteTime);
  97.  
  98.     if (seconds > target) {
  99.         dirEntry.lastArchiveDateAndTime = 0;  /* kludge: create a terminator for name! */
  100.     
  101.             if (reportOnly) {
  102.                 printf("  advice ");
  103.             }
  104.             else {
  105.                 result = PurgeSalvagableFile(serverConnID,directoryHandle,sequenceNumber,
  106.                                             dirEntry.name);
  107.                 if (result) {
  108.                     printf("  DENIED ");
  109.                 }
  110.                 else {
  111.                     printf("         ");
  112.                 }
  113.             }
  114.         printf("%2.2d/%2.2d/%4.2d %2.2d:%2.2d.%2.2d %s\n",
  115.             ts.tm_mon+1, ts.tm_mday, ts.tm_year+1900, ts.tm_hour, ts.tm_min, ts.tm_sec,
  116.             dirEntry.name);
  117.     }
  118. }
  119.  
  120.  
  121. void ExitWithUsage()
  122. {
  123.     fprintf(stderr,"NPURGE  (Version 1.1)  Network Purge\n\n");
  124.     fprintf(stderr,"Usage: npurge [-d <days>] [-h <hours>] [-m <minutes] [-s] [-r] [<root>]\n");
  125.     fprintf(stderr,"          -d  Specify the age of target files in days\n");
  126.     fprintf(stderr,"          -h  Specify the age of target files in hours\n");
  127.     fprintf(stderr,"          -m  Specify the age of target files in minutes\n");
  128.     fprintf(stderr,"          -f  Force file times to midnight (use with -d)\n");
  129.     fprintf(stderr,"          -s  process subdirectories\n");
  130.     fprintf(stderr,"          -r  report only, do not purge files\n");
  131.     fprintf(stderr,"      <root>  specify starting directory\n");
  132.     fprintf(stderr,"   The -d, -h, -m parameters are additive\n");
  133.     exit(255);
  134. }
  135.     
  136.  
  137.  
  138. #define NEXTARG \
  139.     if (*(++(*argv)) == 0) { \
  140.         argv++; \
  141.         argc--; \
  142.     }
  143.  
  144.  
  145.  
  146. void ParseOptions(int argc, char **argv)
  147. {
  148.     while(--argc > 0) {
  149.         ++argv;
  150.         if (**argv == '-') {
  151.             (*argv)++;
  152.             switch (**argv) {
  153.                 case 'd':
  154.                     NEXTARG;
  155.                     target += atof(*argv) * 60 * 60 * 24;
  156.                     useDefaultTarget = 0;
  157.                     break;
  158.                 case 'h':
  159.                     NEXTARG;
  160.                     target += atof(*argv) * 60 * 60;
  161.                     useDefaultTarget = 0;
  162.                     break;
  163.                 case 'm':
  164.                     NEXTARG;
  165.                     target += atof(*argv) * 60;
  166.                     useDefaultTarget = 0;
  167.                     break;
  168.                 case 's':
  169.                     processSubdirectories = 1;
  170.                     break;
  171.                 case 'f':
  172.                     forceMidnight = 1;
  173.                     break;
  174.                 case 'r':
  175.                     reportOnly = 1;
  176.                     break;
  177.                 default:
  178.                     fprintf(stderr,"Unknown option: -%s\n",*argv);
  179.                     ExitWithUsage(); 
  180.             }
  181.         }
  182.         else {
  183.             if (rootDirectory != NULL) {
  184.                 ABORT("Root directory may only be specified once");
  185.             }
  186.             rootDirectory = *argv;
  187.         }                
  188.     }
  189. }
  190.  
  191. void ProcessDirectory(BYTE directoryHandle)
  192. {
  193. BYTE newDirectoryHandle;
  194.  
  195.     GetDirectoryPath(directoryHandle,directoryPathName);
  196.     printf("%s\n",directoryPathName);
  197.  
  198.     GetDirectoryPath(baseDirectoryHandle,savePathname);
  199.  
  200.     if (SetDirectoryHandle(baseDirectoryHandle,directoryPathName,baseDirectoryHandle)) {
  201.         ABORT("Can't change directories");
  202.     }
  203.     
  204.     sequenceNumber = -1;
  205.     while(PurgeFiles(baseDirectoryHandle));
  206.  
  207.     if (SetDirectoryHandle(baseDirectoryHandle,savePathname,baseDirectoryHandle)) {
  208.         ABORT("Can't change directories");
  209.     }
  210. }
  211.  
  212.  
  213.  
  214. main(int argc, char **argv)
  215. {
  216. BYTE newDirectoryHandle;
  217. int result;
  218.  
  219.     if (argc == 1)  ExitWithUsage();
  220.  
  221.     time(¤tTime);
  222.  
  223.     _dos_getdrive(¤tDrive);
  224.     currentDrive--;
  225.     
  226.     if (GetDriveInformation((BYTE)currentDrive,&serverConnID,&baseDirectoryHandle) & 1 != 1) {
  227.         ABORT("Must be run from a network drive");
  228.     }
  229.  
  230.     if (!IsV3Supported(serverConnID)) {
  231.         ABORT("Must be Netware 386");
  232.     }
  233.     
  234.     GetDirectoryPath(baseDirectoryHandle,originalPathname);
  235.     
  236.     ParseOptions(argc,argv);
  237.     
  238.     if (useDefaultTarget) target = defaultTarget;
  239.  
  240.     if (rootDirectory != NULL) {
  241.         if (SetDirectoryHandle(baseDirectoryHandle,rootDirectory,baseDirectoryHandle)) {
  242.             fprintf(stderr,"Possibly bad directory name: %s\n",rootDirectory);
  243.             ABORT("Can't change directories");
  244.         }
  245.     }
  246.     
  247.     ProcessDirectory(baseDirectoryHandle);
  248.     
  249.     if (processSubdirectories) {
  250.         WalkDirectory(baseDirectoryHandle,ProcessDirectory);
  251.     }
  252.  
  253.     if (SetDirectoryHandle(baseDirectoryHandle,originalPathname,baseDirectoryHandle)) {
  254.         ABORT("Can't change directories");
  255.     }
  256. }
  257.